package com.feel.mall.orderservice.controller;

import com.feel.mall.common.bean.Cart;
import com.feel.mall.common.bean.RespBean;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.CartService;
import com.feel.mall.common.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 刘永钦
 * @Description:
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     */
    @PostMapping("/{goodsId}")
    public RespBean addInfo(@PathVariable("goodsId") Integer goodsId, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setGoodsId(goodsId);

        return cartService.addInfo(cart) ? RespBean.ok("添加成功") : RespBean.error("添加失败");

    }

    /**
     * 获得购物车中商品数量
     */
    @GetMapping("/number/{goodsId}")
    public RespBean getCartNumber(@PathVariable("goodsId") Integer goodsId, @RequestHeader("token") String token) {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setGoodsId(goodsId);

        Integer cartNumber = cartService.getCartNumber(cart);

        return RespBean.ok("查询成功", cartNumber);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public RespBean delInfo(@PathVariable("id") Integer id, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        cartService.delInfo(id, userId);

        return RespBean.ok("删除成功");
    }

    /**
     * 修改购物车信息
     */
    @PutMapping
    public RespBean updateInfo(@RequestBody Cart cart, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        cartService.updateInfo(cart, userId);
        return RespBean.ok("修改成功");
    }

    /**
     * 查询
     */
    @GetMapping
    public RespBean getByCondition(@RequestHeader("token") String token) {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Cart cart = new Cart();
        cart.setUserId(userId);

        List<Cart> list = cartService.getByCondition(cart);

        return RespBean.ok("查询成功", list);
    }

    /**
     * 通过id批量查询
     */
    @GetMapping("/idList")
    public RespBean getByIdList(Integer[] cartId, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        List<Cart> list = cartService.getByIdList(cartId, userId);
        return RespBean.ok("查询成功",list);
    }
}
